﻿//初始化
$(document).ready(function(){
	init_focus();
	init_marqueeArticle();
	init_vote();
});

//初始化焦点图
function init_focus(){
	var interv = 5000; //切换间隔时间
	var interv2 = 10; //切换速度
	var opac1 = 80; //文字背景的透明度
	//获取对象
	function getTag(tag,obj){
		if(obj == null){
			return document.getElementsByTagName(tag);
		}else{
			return obj.getElementsByTagName(tag);
		}
	}
	function getid(id){
		return document.getElementById(id);
	};
	var opac=0,j=0,t=63,num,scton=0,timer,timer2,timer3;
	var focuses = $(".focus");
	console.log(focuses.length);
	for(var i=0;i<focuses.length;i++){
		if (window.innerWidth > 800 && i === 0) {
			continue;
		}
		var id = focuses.get(i);
		var li = getTag("li",id);
		var div = document.createElement("div");
		var title = document.createElement("div");
		var span = document.createElement("span");
		var button = document.createElement("div");
		button.className="button";
		for(var i=0;i<li.length;i++){
			var a = document.createElement("a");
			a.innerHTML = i+1;
			a.onclick = function(){
				clearTimeout(timer);
				clearTimeout(timer2);
				clearTimeout(timer3);
				j = parseInt(this.innerHTML)-1;
				scton = 0;
				t = 63;
				opac = 0;
				fadeon();
			};
			a.className = "default";
			a.onmouseover = function(){
				this.className = "cur";
			};
			a.onmouseout = function(){
				this.className = "default";sc(j);
			};
			button.appendChild(a);
		}
	}
	//控制图层透明度
	function alpha(obj,n){
		if(document.all){
			obj.style.filter = "alpha(opacity="+n+")";
		}else{
			obj.style.opacity = (n/100);
		}
	}
	//控制焦点按钮
	function sc(n){
		for(var i=0;i<li.length;i++){
			button.childNodes[i].className = "default";
		};
		button.childNodes[n].className = "cur";
	}
	title.className = "num_list";
	title.appendChild(span);
	alpha(title,opac1);
	id.className = "focus";
	id.appendChild(div);
	id.appendChild(title);
	id.appendChild(button);
	//渐显
	var fadeon = function(){
		opac+=5;
		div.innerHTML=li[j].innerHTML;
		span.innerHTML = getTag("img",li[j])[0].alt;
		alpha(div,opac);
		if(scton == 0){
			sc(j);
			num = -2;
			scrolltxt();
			scton=1;
		};
		if(opac<100){
			timer = setTimeout(fadeon,interv2);
		}else{
			timer2 = setTimeout(fadeout,interv);
		}
	};
	//渐隐
	var fadeout = function(){
		opac-=5;
		div.innerHTML = li[j].innerHTML;
		alpha(div,opac);
		if(scton == 0){
			num = 2;
			scrolltxt();
			scton = 1;
		}
		if(opac > 0){
			timer = setTimeout(fadeout,interv2);
		}else{
			if(j<li.length-1){
				j++;
			}else{
				j = 0;
			}
			fadeon();
		}
	};
	//滚动文字
	var scrolltxt = function(){
		t+=num;
		span.style.marginTop = t+"px";
		if(num<0 && t>3){
			timer3 = setTimeout(scrolltxt,interv2);
		}else if(num>0 && t<62){
			timer3 = setTimeout(scrolltxt,interv2);
		}else{
			scton = 0;
		}
	};
	fadeon();
}


var timeoutPicturenews;

//初始化文章滚动
function init_marqueeArticle(){
	var marqueeItem_article = $(".marqueeItem_article");
    if(marqueeItem_article != null){
    	if($(marqueeItem_article).children().length > 2){
	    	$(".marqueePanel_article").attr("onmouseover", "stopMarquee_article()");
			$(".marqueePanel_article").attr("onmouseout", "startMarquee_article()");
		    marqueeItem_article.append(marqueeItem_article.html());
		    timeoutPicturenews=setTimeout("marquee_article()",1000);
    	}
	}
}

//文章滚动
function marquee_article(){
    var leftScroll = $(".marqueePanel_article").get(0).scrollLeft;
    var offsetWidth = $(".marqueeItem_article").get(0).offsetWidth;
	if(parseInt(leftScroll) < parseInt(offsetWidth) / 2) $(".marqueePanel_article").get(0).scrollLeft++;
	else $(".marqueePanel_article").get(0).scrollLeft = 0;
	timeoutPicturenews=setTimeout("marquee_article()",50);
}

//文章滚动开始
function startMarquee_article(){
    timeoutPicturenews=setTimeout("marquee_article()",50);
}

//文章滚动暂停
function stopMarquee_article(){
    clearTimeout(timeoutPicturenews);
}

//投票初始化
function init_vote(){
	if($(".voteoptionpanel table tr").length > 0){
		$.ajax({
			type: "get",
			url: "/vote/getpoll",
			dataType: "json",
			cache: false,
			success: function(data){
				var voteoptiontotal = 0;
				for(var i=0; i<data.length; i++){
					var voteoption = data[i];
					voteoptiontotal += voteoption.poll;
				}
				if (voteoptiontotal > 0){
					for(var i=0; i<data.length; i++){
						var voteoption = data[i];
						$("#voteoptionrate" + voteoption.id).width(parseFloat(voteoption.poll) / voteoptiontotal * 100);
					}
				}
			}
		});
	}
}

//投票表单验证
function checkvoteform(){
	return checknullRd("voteoptionid", "投票选项");
}

//tab切换
function swtichTing(id){
	var images = $(".tinghead img");
	for(i=0;i<images.length;i++) $(images[i]).attr("src",$(images[i]).attr("src").replace("_h",""));
	$("#h_"+id).attr("src","/template/default/image/ting/"+id+"_h.gif");
	$(".tingxzdivcontents div").hide();
	$("#xz_"+id).show();
}

function swtichjcdt(obj, id) {
    $(obj).siblings().removeClass("articletitlecur");
    $(obj).addClass("articletitlecur");
    $(".jcdtxwfbhgyssdiv").hide();
    $("#" + id).show();
    $("#jctdmore")[0].href = $(obj)[0].href;
    if (window.innerWidth <= 800) return false;
}

function swtichgzlc(obj, id) {
    $(obj).siblings().removeClass("articletitlecur");
    $(obj).addClass("articletitlecur");
    $(".gzlcjwxzdiv").hide();
    $("#" + id).show();
	$("#gzlcmore")[0].href = $(obj)[0].href;
	if (window.innerWidth <= 800) return false;
}

function swtichgzbg(obj, id) {
    $(obj).siblings().removeClass("articletitlecur");
    $(obj).addClass("articletitlecur");
    $(".gzbgczxxdiv").hide();
    $("#" + id).show();
	$("#gzbgmore")[0].href = $(obj)[0].href;
	if (window.innerWidth <= 800) return false;
}

function swtichflws(obj, id) {
    $(obj).siblings().removeClass("articletitlecur");
    $(obj).addClass("articletitlecur");
    $(".flwssssczyajdiv").hide();
    $("#" + id).show();
	$("#flwsmore")[0].href = $(obj)[0].href;
	if (window.innerWidth <= 800) return false;
}
